package com.oa.framework.acc.account.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 用户账号
 *
 * @author cuichao
 */
@Getter
@Setter
@TableName(value = "acc_user_account")
public class UserAccount extends UserSecurityInfo {
    /**
     * 账号主要身份ID
     */
    @TableField(value = "priority_principal")
    private Long priorityPrincipalId;
    /**
     * 用户主要身份
     */
    @TableField(exist = false)
    private AccountPrincipal priorityPrincipal;
    /**
     * 用户主要身份
     */
    @TableField(exist = false)
    private List<AccountPrincipal> principals;


    /**
     * 获取用户的主要身份
     *
     * @return
     */
    public AccountPrincipal getPriorityPrincipal() {
        if(priorityPrincipal != null){
            return priorityPrincipal;
        }
        if (priorityPrincipalId == null) {
            return null;
        }
        if (CollectionUtils.isEmpty(principals)) {
            return null;
        }
        Optional<AccountPrincipal> optional = principals.stream().filter(v -> priorityPrincipalId.equals(v.getPrincipalId())).findFirst();
        priorityPrincipal =  optional.orElse(null);
        return priorityPrincipal;
    }



}
